import request from '@/utils/request'

// 得到具体单个文章
export const getArticle = (id) => {
  return request({
    url: '/articles/' + id
  })
}
// 关注和不关注作者
export const followAuthor = (authorId, isFollow) => {
  if (isFollow) {
    return request({
      url: '/user/followings/',
      method: 'POST',
      data: { target: `${authorId}` }
    })
  } else {
    console.log('noFollow')
    return request({
      url: '/user/followings/' + authorId,
      method: 'delete'
    })
  }
}

// 获取文章的评论
// * @param {String} articleId - 文章ID
// * @param {String} offset - 上一页数据最后一个ID，做为下一页请求的偏移量
export const getCommentsByArticle = (articleId, offset) => {
  return request({
    url: '/comments',
    params: {
      type: 'a',
      source: articleId,
      offset
    }
  })
}

// * 点赞文章，取消点赞
// * @param {String} articleId - 文章ID
// * @param {Boolean} isLike - 是否点赞
export const likeArticle = (articleId, isLike) => {
  if (isLike) {
    return request({
      url: '/article/likings',
      method: 'post',
      data: { target: articleId }
    })
  } else {
    return request({
      url: '/article/likings/' + articleId,
      method: 'delete'
    })
  }
}

// 对评论点赞
export const likeComment = (articleId, isLike) => {
  if (isLike) {
    return request({
      url: '/comment/likings',
      method: 'post',
      data: { target: articleId }
    })
  } else {
    return request({
      url: '/comment/likings/' + articleId,
      method: 'delete'
    })
  }
}

// * 收藏文章，取消收藏
// * @param {String} articleId - 文章ID
// * @param {Boolean} isCollect - 是否收藏

export const collectArticle = (articleId, isCollect) => {
  if (isCollect) {
    return request({
      url: '/article/collections',
      method: 'post',
      data: { target: articleId }
    })
  } else {
    return request({
      url: '/article/collections/' + articleId,
      method: 'delete'
    })
  }
}

// * 获取评论回复
// * @param {String} commentId - 评论ID
// * @param {String} offset - 上一页数据最后一个ID，做为下一页请求的偏移量
export const getReplyByComment = (commentId, offset) => {
  return request({
    url: '/comments',
    params: { type: 'c', source: commentId, offset }
  })
}

// * 对文章进行评论，对评论进行回复
// * @param {*} target - 评论：文章ID，回复：评论ID
// * @param {*} content - 内容
// * @param {*} articleId - 回复：文章ID
// * @returns
export const commentOrReply = (target, content, articleId = null) => {
  return request({
    url: '/comments',
    method: 'post',
    data: { target, content, art_id: articleId }
  })
}

// 对单个评论进行点赞
// @params -点赞的评论id --必须
